import React from 'react'
import Code from './Code'
import Tree, { walk } from '../ui-components/tree'
import style from './style'
import util from '../util'
import cx from 'classnames'

export default 
class extends React.Component {

  state = {
    root: {
      name: 'node-1',
      id: 0,
      children: [
        {
          name: 'node-1-1',
          id: 1,
          children: [
            {
              name: 'node-1-1-1',
              id: 2,
              handler: () => {
                this.setState({
                  root: walk(this.state.root, 2, children => {
                    if (util.isNull(children)) {
                      return [
                        {
                          name: 'node-1-1-1-1'
                        }
                      ]
                    }
                  }, {})
                })
              },
              children: [
                {
                  name: 'node-1-1-1-1'
                }
              ]
            }
          ]
        }
      ]
    }
  }

  update() {
    this.setState({
      root: {
        ...state.root,
        children: state.root.children.map((item) => {
          if (item.id === id) {
            return {
              ...item,
              children: []
            }
          }
          else {
            if (item.children) {
              return {
                ...item,
                children: item.children.map
              }
            }
          }
        })
      }
    })
  }

  render() {
    return (
      <div className={cx({
        [style.root]: true
      })}>
        <Code>{'<Tree/>'}</Code>
        <div>
          <Tree root={this.state.root}/>
        </div>
      </div>
    )
  }
}
